/*

* Copyright (c) 2007, National University of Singapore (NUS)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions 
* are met:
*
*   * Redistributions of source code must retain the above copyright notice, 
*     this list of conditions,the authors and the following disclaimer.
*   * Redistributions in binary form must reproduce the above copyright notice,
*     this list of conditions,the authors and the following disclaimer in
*     the documentation and/or other materials provided with the distribution.
*   * Neither the name of the university nor the names of its 
*     contributors may be used to endorse or promote products derived from 
*     this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
* POSSIBILITY OF SUCH DAMAGE.
* 
* Author: Wenwei Xue (dcsxw@nus.edu.sg)
*
*/

/**
 * This class represents a context domain that a physical space belongs to.
 */
package psg.servicemanager;
public class ContextDomain {
	
	String name; // name of the context domain
	
	ContextAttribute contextAttrs [] = null;  // list of context attributes supported in the domain
	
	/**
	 * The list of context domains currently supported by the PSG simulator.
	 */
	public static String [] domainNames = {"person",
		"shop",
		"office",
		"home",
	};
	
	/**
	 * This function examines whether a given name for a context domain is valid.
	 * @param name a given name
	 * @return TRUE if the input is the valid name for a context domain; otherwise FALSE
	 */
	public static boolean checkDomain(String domain) {
		
		domain = domain.toLowerCase();
		
		int i;
		for (i=0;i<domainNames.length;i++)
			if (domain.equals(domainNames[i].toLowerCase()))
				return true;
				
		return false;	
				
	}  // checkDomain
	
	/**
	 * This function examines whether a given attribute is valid for a context domain.
	 * @param domain name of a context domain
	 * @param attrName name of a context attribute in the domain
	 * @return TRUE if the attribute is valid for the domain; otherwise FALSE
	 */
	public static boolean checkName(String domain, String attrName) {
		
		if (!checkDomain(domain))
			return false;
		
		String attrs [] = getAttributes(domain);
		
		attrName = attrName.toLowerCase();
		
		int i;
		for (i=0;i<attrs.length;i++)
			if (attrName.equals(attrs[i].toLowerCase()))
				return true;
				
		return false;
		
	}	// checkName
	
	/**
	 * This function gets the list of context attributes in a context domain.
	 * @param domain name of the context domain
	 * @return the list of attributes in the domain
	 */
	public static String [] getAttributes (String domain) {
		
		domain = domain.toLowerCase();
		
		String attrs [] = null;
		
		if (domain.equals(domainNames[0].toLowerCase()))
		{
			attrs = new String [12];
			attrs[0] = ContextAttribute.attrNames[0];
			attrs[1] = ContextAttribute.attrNames[9];
			attrs[2] = ContextAttribute.attrNames[10];
			attrs[3] = ContextAttribute.attrNames[11];
			attrs[4] = ContextAttribute.attrNames[12];
			attrs[5] = ContextAttribute.attrNames[13];
			attrs[6] = ContextAttribute.attrNames[14];
			attrs[7] = ContextAttribute.attrNames[15];
			attrs[8] = ContextAttribute.attrNames[16];
			attrs[9] = ContextAttribute.attrNames[17];
			attrs[10] = ContextAttribute.attrNames[18];
			attrs[11] = ContextAttribute.eventNames[0];
			//attrs[4] = ContextAttribute.attrNames[7];
		}
		else if (domain.equals(domainNames[1].toLowerCase()))
		{
			attrs = new String [7];
			attrs[0] = ContextAttribute.attrNames[0];
			attrs[1] = ContextAttribute.attrNames[2];
			attrs[2] = ContextAttribute.attrNames[4];
			attrs[3] = ContextAttribute.attrNames[5];
			attrs[4] = ContextAttribute.attrNames[6];
//			attrs[5] = ContextAttribute.eventNames[1];
//			attrs[6] = ContextAttribute.eventNames[2];
		}
		else if (domain.equals(domainNames[2].toLowerCase()))
		{
			attrs = new String [5];
			attrs[0] = ContextAttribute.attrNames[0];
			attrs[1] = ContextAttribute.attrNames[1];
			attrs[2] = ContextAttribute.attrNames[2];
			attrs[3] = ContextAttribute.eventNames[0];
//			attrs[4] = ContextAttribute.eventNames[2];
		}
		
		else if (domain.equals(domainNames[3].toLowerCase()))
		{
			attrs = new String [10];
			attrs[0] = ContextAttribute.attrNames[0];
			attrs[1] = ContextAttribute.attrNames[1];
			attrs[2] = ContextAttribute.attrNames[2];
			attrs[3] = ContextAttribute.attrNames[3];
			attrs[4] = ContextAttribute.attrNames[4];
			attrs[5] = ContextAttribute.attrNames[5];
			attrs[6] = ContextAttribute.attrNames[6];
			attrs[7] = ContextAttribute.eventNames[0];
			attrs[8] = ContextAttribute.attrNames[7];
			attrs[9] = ContextAttribute.attrNames[8];
		}
		
		/*for (int i=0;i<attrs.length;i++)
			System.out.println(attrs[i]);
		System.exit(0);*/
		
		return attrs;
		
	}  // getAttributes	
	
	/**
	 * This function gets the list of events defined in a context domain.
	 * @param domain name of the context domain
	 * @return the list of events in the domain
	 */
	public static String [] getEvents (String domain) {
		
		domain = domain.toLowerCase();
		
		String events [] = null;
		
		if (domain.equals(domainNames[0].toLowerCase()))
		{
			events = new String [1];
			
			events[0] = ContextAttribute.eventNames[2];
			
			return events;
		}
		else if (domain.equals(domainNames[1].toLowerCase()))
		{
			events = new String [2];
			events[0] = ContextAttribute.eventNames[1];
			events[1] = ContextAttribute.eventNames[2];		
		}
		else if (domain.equals(domainNames[2].toLowerCase()))
		{
			events = new String [2];
			events[0] = ContextAttribute.eventNames[0];
			events[1] = ContextAttribute.eventNames[2];		
		}
		else if (domain.equals(domainNames[3].toLowerCase()))
		{
			events = new String[1];
			events[0] = ContextAttribute.eventNames[0];
		}
				
		
		return events;
		
	}  // getEvents
	
	
	/**
	 * This function examines whether a given name represents a valid event in a context domain.
	 * @param domain name of a context domain
	 * @param name a given name
	 * @return TRUE if the event is valid for the domain; otherwise FALSE
	 */
	public static boolean checkEvent(String domain, String name) {
		
		String events [] = getEvents(domain);
		
		for (int i=0;i<events.length;i++)
			if (name.toLowerCase().equals(events[i].toLowerCase()))
				return true;
		
		return false;
		
	}	// checkEvent
}